<#include "../common/common.html"/> <@layout>
<script type="text/javascript">
    var url;
    var pid=0;
    /**
     * 打开新增用户的表单，录入用户信息
     */
    function newModel() {
        $('#dlg').dialog('setTitle', '新增模型').dialog('open');
        $('#myform').form('clear');
        reloadCombtree();
        url = '${ctx}/codes/save/${tbName}';
    }
    /**
     * 编辑选中的用户信息
     */
    function editModel() {
        $('#myform').form('clear');

        var row = $('#dt-grids').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('setTitle', '修改模型').dialog('open');
            pid=row.pid;
            reloadCombtree();
            $('#myform').form('load', row);
            url = '${ctx}/codes/update/${tbName}';
        } else {
            $.messager.show({
                title : '提示',
                msg : '请先选择模型，再进行修改。',
                timeout:2000
            });
        }
    }

    function deleteModel() {
        var row = $('#dt-grids').datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认', '您确定要删除选中的模型吗?', function(r) {
                if (r) {
                    $.post('${ctx}/codes/delete/${tbName}-' + row.id, function(data) {
                        $.messager.show({
                            title : '提示',
                            msg : data,
                            timeout:2000
                        });
                        $('#dt-grids').datagrid('reload'); // reload the user data
                    },'text');
                }
            });
        }else {
            $.messager.show({
                title : '提示',
                msg : '请先选择模型，再进行删除。',
                timeout:2000
            });
        }
    }
    /**
     * 保存新增的用户数据，并对登录密码进行校验
     */
    function saveModel() {
        $('#myform').form('submit', {
            url : url,
            onSubmit : function() {
                return $(this).form('validate');
            },
            success : function(data) {
                $.messager.show({ // show error message
                    title : '提示',
                    msg : data,
                    timeout:2000
                });
                $('#dlg').dialog('close');
                $('#dt-grids').datagrid('reload');
            }
        });
    }

    function query() {
        $('#dt-grids').datagrid('load', $.getQueryParam('queryTable'));
        $('#qdlg').dialog('close');
    }

    function queryModel() {
        $('#qdlg').dialog('setTitle', '查询条件').dialog('open');
    }

    function reloadCombtree(){
        <#list fdsList as m>
        <#if m.id != "pid">$('#${m.id}').combotree('reload').combotree('setValue',0);</#if>
        </#list>
    }
    $(function(){
        <#list fdsList as m>
        <#if m.id == "type">
        $('#${m.id}').combotree({

            url:"${ctx}/${m.url}",
            onSelect: function(rec){
                $('#pid').combotree({
                    url:"/common/comboTree/type='"+rec.id+"'?table=codes&fields=code as id,name as text&pid=pid&pidStart=0&id=id"
                }).combotree('setValue',pid);
            }
        });
        <#elseif m.id != "pid">
        $('#${m.id}').combotree({
            url:"${ctx}/${m.url}"
        });
        </#if>
        </#list>
        <#list qFdsList as m>
        <#if m.id == "type">
        $('#q_${m.id}').combotree({
            url:"${ctx}/${m.url}",
            onSelect: function(rec){
                $('#q_pid').combotree({
                    url:"/common/comboTree/type='"+rec.id+"'?table=codes&fields=code as id,name as text&pid=pid&pidStart=0&id=id"
                });
            }
        });
        <#elseif m.id != "pid">
        $('#q_${m.id}').combotree({
            url:"${ctx}/${m.url}"
        });
        </#if>
        </#list>
        $('#dt-grids').datagrid({
            columns:[[
            <#list list as m>
                {field:'${m.fieldname}',title:'${m.fielddesc}',sortable:true,width:100<#if m.data??>,data:${m.data},formatter: function(value,row){
                    var data = this.data;
                    for(var i=0,len=data.length;i<len;i++){
                        if(this.field=='pid'){
                            if(row.type == data[i].type && value == data[i].id){
                                return data[i].text;
                            }
                        }else{
                            if(value == data[i].id){
                                return data[i].text;
                            }
                        }
                    }
                }</#if>}<#if m_has_next>,</#if>
            </#list>
            ]]
        });
    });
</script>
<body>
	<table id="dt-grids" class="easyui-datagrid"
		data-options="fit:true,singleSelect:true,rownumbers:true,pagination:true,toolbar:'#toolbar',url:'${ctx}/codes/query/${tbName}'">
		<!--<thead>
			<tr>
                <#list list as m><th data-options="field:'${m.fieldname}',width:200">${m.fielddesc}</th></#list>
			</tr>
		</thead>-->
	</table>

	<div id="toolbar">
		<a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-add',plain:true" onclick="newModel()">新增</a>
		<a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-edit',plain:true" onclick="editModel()">编辑</a>
		<a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-remove',plain:true"
			onclick="deleteModel()">删除</a> <a href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-search',plain:true"
			onclick="queryModel()">查询</a>
	</div>
	<div id="dlg" class="easyui-dialog"
		style="top:60%;left:60%;width: 500px; height: 400px; padding: 10px"
		data-options="closed:true,modal:true,buttons:'#dlg-buttons'">
		<form id="myform" method="post">
			<input type="hidden" name="id" />
			<table cellpadding="0" cellspacing="0" class="form-table"
				align='center'>
				<#list cu as m> <#if m_index%2==0>
				<tr>
					</#if>
					<td style="width: 100px">${m.fielddesc}：</td>
					<td style="width: 180px"><#if m.datatype=="timestamp"><input
						id="${m.fieldname}" name="${m.fieldname}"
						class="easyui-datetimebox"<#if
						m.isnull=="no">data-options="required:true"</#if> type="text"
						style="width: 150px"/> <#elseif m.datatype=="date"><input
						id="${m.fieldname}" name="${m.fieldname}" class="easyui-datebox"<#if
						m.isnull=="no">data-options="required:true"</#if> type="text"
						style="width: 150px"/> <#else><input id="${m.fieldname}"
						name="${m.fieldname}"<#if m.isnull=="no"><#if
						!m.cmb>class="easyui-validatebox"</#if>
						data-options="required:true"</#if> type="text" style="width:
						150px"/></#if>
					</td><#if m_index%2==1>
				</tr>
				</#if> </#list>
			</table>
		</form>
		<div id="dlg-buttons" style="text-align: center">
			<a href="#" class="easyui-linkbutton" iconCls="icon-save"
				onclick="saveModel()">保存</a> <a href="#" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
		</div>
	</div>
	<div id="qdlg" class="easyui-dialog"
		style="width: 500px; height: 350px; padding: 10px" closed="true"
		modal="true" buttons="#query-buttons">
		<table id='queryTable' cellpadding="0" cellspacing="0"
			class="form-table" align='center'>
			<#list query as m> <#if m_index%2==0>
			<tr>
				</#if>
				<td style="width: 80px">${m.fielddesc}：</td>
				<td style="width: 180px"><input id="q_${m.fieldname}"
					name="search_EQ_${m.fieldname}" class="query" style="width: 150px" /></td>
				<#if m_index%2==1>
			</tr>
			</#if> </#list>
		</table>
		<div id="query-buttons" style="text-align: center">
			<a href="#" class="easyui-linkbutton" iconCls="icon-save"
				onclick="query()">查询</a> <a href="#" class="easyui-linkbutton"
				iconCls="icon-cancel"
				onclick="javascript:$('#qdlg').dialog('close')">取消</a>
		</div>
	</div>
</body>
</@layout>
